import { auths as _auths } from './data.js';

let auths = _auths;

// 权限列表
const getAuthList = function () {
  return auths;
};

// 编辑权限
const editAuth = function (id, detail) {
  const copiedAuth = [...auths];
  const target = auths.findIndex((c) => c.id === id);
  if (copiedAuth[target]) {
    copiedAuth[target] = detail;
  }
  return copiedAuth;
};

// 新增权限
const addAuth = function (newAuth) {
  auths.unshift(newAuth);
  return auths;
};

//搜索权限
const searchAuth = function (query) {
  if(query.id === undefined && query.name === undefined) {
    return auths;
  } else {
    let result = auths.filter(auth => auth.id === query.id || auth.name === query.name);
    return result;
  }
};

//删除权限
const deleteAuth = function (id) {
  auths = auths.filter(auth => auth.id !== id);
};

export { getAuthList, editAuth, addAuth, searchAuth, deleteAuth };
